﻿/********************************************************************************/
// <copyright file="Globals.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="17/12/2012 4:24:18 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Application
{
    using System;
    using JLL.China.Infrastructure.Configuration;

    /// <summary>
    /// Read web.config configuration key. Throw exception if key was not found    
    /// Contains static properties which are used globally throughout
    /// the application.
    /// </summary>
    public static class Globals
    {
        #region -- Public Static Methods --
        public static DateTime GetDateTimeConfig(IConfigurationReader configReader, string configKey, object defaultValue)
        {
            string str = configReader.ReadConfiguration(configKey);
            DateTime result;
            if (string.IsNullOrEmpty(str) || DateTime.TryParse(str, out result) == false)
            {
                throw new ApplicationException(string.Format(ConfigurationReader.ErrorMessage, configKey, defaultValue));
            }
            else
            {
                return result;
            }
        }

        public static string GetStringConfig(IConfigurationReader configReader, string configKey, object defaultValue)
        {
            string str = configReader.ReadConfiguration(configKey);
            if (string.IsNullOrEmpty(str))
            {
                throw new ApplicationException(string.Format(ConfigurationReader.ErrorMessage, configKey, defaultValue));
            }
            else
            {
                return str;
            }
        }

        public static int GetIntConfig(IConfigurationReader configReader, string configKey, int defaultValue)
        {
            string str = GetStringConfig(configReader, configKey, defaultValue);
            int result = defaultValue;
            if (int.TryParse(str, out result) == false)
            {
                return defaultValue;
            }
            else
            {
                return result;
            }
        }

        public static bool GetBooleanConfig(IConfigurationReader configReader, string configKey, bool defaultValue)
        {
            string str = GetStringConfig(configReader, configKey, defaultValue);
            bool result = defaultValue;
            if (bool.TryParse(str, out result) == false)
            {
                return defaultValue;
            }
            else
            {
                return result;
            }
        }

        public static DateTime GetDateTimeConfig(string configKey, object defaultValue)
        {
            return GetDateTimeConfig(ConfigurationReader, configKey, defaultValue);
        }

        public static string GetStringConfig(string configKey, object defaultValue)
        {
            return GetStringConfig(ConfigurationReader, configKey, defaultValue);
        }

        public static int GetIntConfig(string configKey, int defaultValue)
        {
            return GetIntConfig(ConfigurationReader, configKey, defaultValue);
        }

        public static bool GetBooleanConfig(string configKey, bool defaultValue)
        {
            return GetBooleanConfig(ConfigurationReader, configKey, defaultValue);
        }
        #endregion

        public static IConfigurationReader ConfigurationReader { get; set; }
    }
}
